package co.baiku.boot.config;

import co.baiku.boot.common.tools.StringTools;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.github.wxpay.sdk.IWXPayDomain;
import com.github.wxpay.sdk.WXPayConfig;
import com.github.wxpay.sdk.WXPayDomainImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.context.properties.ConfigurationProperties;

import java.io.*;

/**
 * 微信支付
 */
@ConfigurationProperties(prefix = "ajavaer.weixin.pay")
public class WeixinPayConfig implements Serializable, InitializingBean {
    private String certPath;//证书 pkcs12格式 路径
    private String appid;//微信分配的公众账号ID 或者 小程序的appid ，看具体情况
    private String mchid;//微信支付分配的商户号
    private String secret;//API 密钥
    private String subAppID;//当前调起支付的小程序APPID
    private String subMchID;//微信支付分配的子商户号
    private String notifyUrl;//异步通知地址
    private String refundNotifyUrl;//微信退款回调地址
    private Boolean useSandbox = false;

    @JsonIgnore
    private byte[] certData;
    @JsonIgnore
    private Logger log = LoggerFactory.getLogger(WeixinPayConfig.class);

    public WXPayConfig toWXPayConfig() {
        final WeixinPayConfig config = this;
        return new WXPayConfig() {
            @Override
            public String getAppID() {
                return config.getAppid();
            }

            @Override
            public String getMchID() {
                return config.getMchid();
            }

            @Override
            public String getKey() {
                return config.getSecret();
            }

            @Override
            public InputStream getCertStream() {
                return config.getCertStream();
            }

            @Override
            public IWXPayDomain getWXPayDomain() {
                return WXPayDomainImpl.instance();
            }
        };
    }

    @JsonIgnore
    public InputStream getCertStream() {
        return new ByteArrayInputStream(this.certData);
    }
    
    @Override
    public void afterPropertiesSet() throws Exception {
        if (StringTools.isNotBlank(certPath)) {
            File file = new File(certPath);
            try (InputStream certStream = new FileInputStream(file)) {
                this.certData = new byte[(int) file.length()];
                certStream.read(this.certData);
            }
        } else {
            log.error("微信证书为空,当前微信支付不可用");
        }

    }

    public String getCertPath() {
        return certPath;
    }

    public void setCertPath(String certPath) {
        this.certPath = certPath;
    }

    public String getAppid() {
        return appid;
    }

    public void setAppid(String appid) {
        this.appid = appid;
    }

    public String getMchid() {
        return mchid;
    }

    public void setMchid(String mchid) {
        this.mchid = mchid;
    }

    public String getSecret() {
        return secret;
    }

    public void setSecret(String secret) {
        this.secret = secret;
    }

    public String getSubAppID() {
        return subAppID;
    }

    public void setSubAppID(String subAppID) {
        this.subAppID = subAppID;
    }

    public String getSubMchID() {
        return subMchID;
    }

    public void setSubMchID(String subMchID) {
        this.subMchID = subMchID;
    }

    public String getNotifyUrl() {
        return notifyUrl;
    }

    public void setNotifyUrl(String notifyUrl) {
        this.notifyUrl = notifyUrl;
    }

    public Boolean getUseSandbox() {
        return useSandbox;
    }

    public void setUseSandbox(Boolean useSandbox) {
        this.useSandbox = useSandbox;
    }

    public byte[] getCertData() {
        return certData;
    }

    public void setCertData(byte[] certData) {
        this.certData = certData;
    }

    public String getRefundNotifyUrl() {
        return refundNotifyUrl;
    }

    public void setRefundNotifyUrl(String refundNotifyUrl) {
        this.refundNotifyUrl = refundNotifyUrl;
    }
}
